<?php
class UserController extends AdminModuleController 
{
	public function actionAdmin() {
		$page = max(1, getgpc('page'));
		
		$sqladd = $pageUrl = '';
		$userName = getgpc('userName', 'R');
		$realName = getgpc('realName', 'R');
		if ($userName) {
			$sqladd = " userName like '%$userName%' AND";
		}
		if ($realName) {
			$sqladd = " realName like '%$realName%' AND";
		}
		$groupId = getgpc('groupId', 'R');
		if(getgpc(2)) {
			$sqladd = " groupId<'".GODHOUSE_GROUP_ADMIN_LIMIT."' AND";
			$pageUrl = "user-admin-adminList.htm";
		} else {
			if(is_numeric($groupId)) {
				$sqladd = " groupId='$groupId' AND";
			}
			$pageUrl = "user-admin.htm?groupId=$groupId&userName=$userName";
		}
		$sqladd = substr($sqladd, 0, -3);
		//echo $sqladd;
		$userList = $this->UserModel->findAll($sqladd, $page, GODHOUSE_PPP3);
		foreach($userList as &$v) {
			$userGroup = $this->UserGroupModel->findByPk($v['groupId']);
			$ordersId = $this->UserModel->findByPk($v['userId']);
		
		
			$ordersamounts = $this->OrderModel->count(" userId='".$ordersId['userId']."' and orderStatus!='3'");			
			$v['amounts'] = $ordersamounts;
			
			$v['group'] = $userGroup['groupName'];
			$v['createdTime2'] = date('Y-m-d H:i:s', $v['createdTime']);
			$v['statusText'] = Lookup::item('status', $v['status']);
			
			/* 所属用户 */
			if($v['parentId']) {
				$user = $this->UserModel->findByPk($v['parentId']);
				$v['parentName'] = $user['userName'];
			} else {
				$button = '';
				if($v['groupId']>=GODHOUSE_GROUP_ADMIN_LIMIT) {
					$button = '<a href="user-assign-'.$v['userId'].'.htm" onclick="ajaxmenu(this.href, 1)">未归纳</a>';
				}
				$v['parentName'] = $button;
			}
			
			/* 地区 */
			$v['area'] = '';
			$area = $this->AreaModel->findByPk($v['areaId']);
			if($area) {
				$pArea = $this->AreaModel->findByPk($area['fId']);
				$v['area'] = $pArea['name'] .'->'. $area['name'];
			}
		}
		$num = $this->UserModel->count($sqladd);
		$multipage = page($num, GODHOUSE_PPP3, $page, $pageUrl);
		
		$groupList = $this->UserGroupModel->findAll("groupId>='".GODHOUSE_GROUP_ADMIN_LIMIT."'");
		
		$this->view->assign('userName', $userName);
		$this->view->assign('groupId', $groupId);
		$this->view->assign('groupList', $groupList);
		$this->view->assign('userList', $userList);
		$this->view->assign('multipage', $multipage);
		$this->display('user_admin');
	}
	
	public function actionCreate() {
		
		if(isset($_POST['user'])) {
			$user = $_POST['user'];
			if(!$user['userName'] || !$user['passwd']) {
				$this->alert('用户名或密码不能为空。', 'BACK');
			}
			$u = $this->UserModel->find("userName='".$user['userName']."'");
			if($u) {
				$this->alert('用户名已存在。', 'BACK');
			}
			
			$parentName = $user['parentId'];
			$user['parentId'] = 0;
			if($parentName) {
				$u = $this->UserModel->find("userName='$parentName'");
				if(!$u) {
					$this->alert('所属管理员未查找到，请核实后再输入。', 'BACK');
				}
				$user['parentId'] = $u['userId'];
			}
			$user['createdTime'] = $this->time;
			$user['passwd'] = md5($user['passwd']);
			$user['createdUser'] = $this->userId;
			$user['status'] = GODHOUSE_STATUS_VERIFIED;
			
			$this->UserModel->insert($user);
			$this->message('创建成功。', GODHOUSE_DOMAIN_WWW."admin/user-admin.htm");
		}
		
		$groups = $this->UserGroupModel->findAll();
		$this->view->assign('groups', $groups);
		
		$areaList = $this->AreaModel->findAll("fId='1'", 0, 0, "id ASC");
		$this->view->assign('areaList', $areaList);
		
		$this->view->assign('action', 'create');
		$this->display('user_create');
	}
	
	public function actionUpdate() {
		$userId = getgpc(2);
		if(isset($_POST['user'])) {
			$user = $_POST['user'];
			if(!$user['passwd']) {
				unset($user['passwd']);
			} else {
				$user['passwd'] = md5($user['passwd']);
			}
			
			$parentName = $user['parentId'];
			$user['parentId'] = 0;
			if($parentName) {
				$u = $this->UserModel->find("userName='$parentName'");
				if(!$u) {
					$this->alert('所属管理员未查找到，请核实后再输入。', 'BACK');
				}
				$user['parentId'] = $u['userId'];
			}
			
			$this->UserModel->updateByPk($userId, $user);
			
			//修改购物车中还没有支付的商品的折扣
			/*
			$userIdStr = '';
			$gArr = $this->UserGroupModel->find("groupId='".$user['groupId']."'");
			$uArr = $this->UserModel->fetchAll("groupId = '".$user['groupId']."'");
			foreach($uArr as $v) {
				$userIdStr .= $v['userId'].',';
			}
			$this->ShoppingcartModel->update("userId IN (".rtrim($userIdStr,',').") AND settleStatus='0'", array('discount'=>$gArr['discount']/100));		
			*/
			
			$this->message('修改成功。', GODHOUSE_DOMAIN_WWW."admin/user-update-$userId.htm");
		}
		
		$user = $this->UserModel->findByPk($userId);
		if($user['parentId']) {
			$u = $this->UserModel->findByPk($user['parentId']);
			$user['parentName'] = $u['userName'];
		}
		
		$areaList = $this->AreaModel->findAll("fId='1'", 0, 0, "id ASC");
		$subAreaList = $area = array();
		if($user['areaId']) {
			$area = $this->AreaModel->findByPk($user['areaId']);
			if($area) {
				$subAreaList = $this->AreaModel->findAll("fId='".$area['fId']."'");
			}
		}
		
		$userGroupList = $this->UserGroupModel->findAll();
		$groups = $userGroupList;
		
		$this->view->assign('area', $area);
		$this->view->assign('areaList', $areaList);
		$this->view->assign('subAreaList', $subAreaList);
		$this->view->assign('user', $user);
		$this->view->assign('groups', $groups);
		$this->view->assign('action', 'update');
		$this->display('user_update');
	}
	
	public function actionAudit() {
		$userId = getgpc(2);
		$status = getgpc(3);
		
		$user = $this->UserModel->findByPk($userId);
		if($user) {
			if(!$user['parentId']) {
				$this->message('请先归纳在管理员名下，再审核。', GODHOUSE_DOMAIN_WWW."admin/user-admin.htm");
			}
			
			switch($status) {
				case GODHOUSE_STATUS_VERIFIED:
					if($user['status'] != GODHOUSE_STATUS_UNVERIFIED && $user['status'] != GODHOUSE_STATUS_PAUSED) {
						$this->message('只有待审核和暂停的用户才能被审核。', GODHOUSE_DOMAIN_WWW."admin/user-admin.htm");
					}
					$this->UserModel->updateByPk($userId, array(
						'status'=>GODHOUSE_STATUS_VERIFIED,
					));
					if($user['status'] == GODHOUSE_STATUS_UNVERIFIED) {
						// 发送短信提醒
						$message = "恭喜您{$user['userName']}，通过了波普瑞网站审核，请您登录网站http://www.brt-e.com进行下一步操作。";
						$mobile = $user['mobile'];
						$errorNo = $this->sendSMS($message, $mobile);
					}
					$this->message('审核操作成功。', GODHOUSE_DOMAIN_WWW."admin/user-admin.htm");
					break;
				case GODHOUSE_STATUS_PAUSED:
					if($user['status'] != GODHOUSE_STATUS_VERIFIED) {
						$this->message('只有已审核的用户才能被暂停。', GODHOUSE_DOMAIN_WWW."admin/user-admin.htm");
					}
					$this->UserModel->updateByPk($userId, array(
						'status'=>GODHOUSE_STATUS_PAUSED,
					));
					$this->message('暂停操作成功。', GODHOUSE_DOMAIN_WWW."admin/user-admin.htm");
					break;
				default:
					$this->message('请您选择要执行的操作。', GODHOUSE_DOMAIN_WWW."admin/user-admin.htm");
					break;
			}
		} else {
			$this->message('审核的用户不存在。', GODHOUSE_DOMAIN_WWW."admin/user-admin.htm");
		}
	}
	
	public function actionDelete() {
		$userId = getgpc(2);
		$this->UserModel->deleteByPk($userId);
		$this->message('删除成功。', GODHOUSE_DOMAIN_WWW."admin/user-admin.htm");
	}
	
	public function actionUpdatePwd() {
		
		if (isset($_POST['submitTo'])) {
			$userId = $this->getState('userId');
			$oldpassword = getgpc('oldpassword', 'P');
			$password = getgpc('password', 'P');
			$password2 = getgpc('password2', 'P');
			if(!$password) {
				$this->alert('新密码不能为空。');
			}
			if($password != $password2) {
				$this->alert('密码不一致。');
			}
			$user = $this->UserModel->find("userId='$userId' AND passwd='".md5($oldpassword)."'");
			if($user) {
				$this->UserModel->updateByPk($userId, array('passwd'=>md5($password2)));
				$this->alert('修改成功。');
			} else {
				$this->alert('原密码不对。');
			}
		}
		$this->display('user_updatePwd');
	}
	
	/* 地区 */
	public function actionAjaxCity() {
		$fId = getgpc(2);
		
		$areaList = $this->AreaModel->findAll("fId='$fId'");
		
		$str = '<select name="user[areaId]" id="city" style="width: 150px;">';
		foreach($areaList as $v) {
			$str .= '<option value="'.$v['id'].'">'.$v['name'].'</option>';
		}
		$str .= '</select>';
		$this->message($str);
	}
	
	/* 分纳在哪个人的名下 */
	public function actionAssign() {
		$userId = getgpc(2);
		if(isset($_POST['submitTo'])) {
			$userName = getgpc('userName', 'P');
			$user = $this->UserModel->findByPk($userName);
			if(!$user) {
				$this->message('所属管理员未查找到，请核实后再输入。', 'user-admin.htm');
			} else {
				$att = array(
					'parentId'=>$user['userId'],
				);
				$this->UserModel->updateByPk($userId, $att);
				$this->message('操作成功', 'user-admin.htm');
			}
		}
		
		$userList = $this->UserModel->findAll("groupId<".GODHOUSE_GROUP_ADMIN_LIMIT);
		
		$this->view->assign('userList', $userList);
		$this->view->assign('userId', $userId);
		$this->display('user_assign');
	}
	
	// 检查用户名是否存在
	public function actionCheckParentUser() {
		$userName = getgpc('userName');
		if(!$userName) {
			$this->message('请输入您的帐号。');
		}
		$user = $this->UserModel->find("userName='$userName'");
		if($user) {
			$this->message('恭喜您“'.$userName.'”可以使用。');
		} else {
			$this->message('输入的所属用户不存在，请重新输入。');
		}
	}
	
	// 检查用户名是否存在
	public function actionCheckUser() {
		$userName = getgpc('userName');
		if(!$userName) {
			$this->message('请输入您的帐号。');
		}
		$user = $this->UserModel->find("userName='$userName'");
		if($user) {
			$this->message('您输入的帐号已存在，请您重新输入。');
		} else {
			$this->message('恭喜您“'.$userName.'”可以注册。');
		}
	}
}
?>